home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / WINDOWS / LENGINE.ZIP / WORLD2.CPP < prev   
Encoding:
C/C++ Source or Header  |  1996-12-11  |  8.0 KB  |  336 lines

  1. #include "header.h"
  2.  
  3. extern DirectDraw *DD;
  4. int Reject(POLYGON *p);
  5. BYTE Darkness[33][256];
  6. BYTE GVects[256][256];
  7.  
  8. WORLD::WORLD()
  9. {
  10.     FILE *fp;
  11.     UpdateMatrix = new MATRIX;
  12.     ImageMap = (BYTE *)GlobalAlloc(GPTR, sizeof(BYTE) * 256 * 256);
  13.     fp = fopen("dark.dat", "rb");
  14.     fread(Darkness, sizeof(BYTE), 33 << 8, fp);
  15.     fclose(fp);
  16.     fp = fopen("gvects.vec", "rb");
  17.     fread(GVects, sizeof(BYTE), 65536, fp);
  18.     fclose(fp);
  19. };
  20.  
  21. WORLD::~WORLD()
  22. {
  23.     GlobalFree((BYTE *)ImageMap);
  24.     delete UpdateMatrix;
  25. };
  26.  
  27. int WORLD::FreeWorld()
  28. {
  29.     int i, j;
  30.  
  31.     delete PolyList;
  32.     for(i = 0; i < Number_LandScapes; i++)
  33.         delete LandScape[i].Vertex;
  34.     for(i = 0; i < Number_Textures; i++)
  35.         Texture[i].Free();
  36. //    delete BitmapObj;
  37.     delete Texture;
  38.     delete Camera;
  39.     return 0;
  40. };
  41.  
  42. // Optimize in assembly
  43. int WORLD::ProjectWorld(long Perspective)
  44. {
  45.     int i, j, p;
  46.     float d, r;
  47.     int sx, ex;
  48.     int sz, ez;
  49.  
  50.     for(i = 0; i < Number_LandScapes; i++)
  51.     {
  52.         sx = Camera[Current_Camera].xTrans;
  53.         sz = Camera[Current_Camera].zTrans;
  54.         sx >>= 10;
  55.         sz >>= 10;
  56.         ex = ( ((sx + X_DIR) < LandScape[i].Width) ? (sx + X_DIR) : (LandScape[i].Width - 1));
  57.         sx = ( ((sx - X_DIR) < 0) ? (0) : (sx - X_DIR));
  58.         ez = ( ((sz + Z_DIR) < LandScape[i].Height) ? (sz + Z_DIR) : (LandScape[i].Height - 1));
  59.         sz = ( ((sz - Z_DIR) < 0) ? (0) : (sz - Z_DIR));
  60.  
  61.         for(int v = sx; v < ex; v++)
  62.         {
  63.             for(int w = sz; w < ez; w++)
  64.             {
  65.                 p = (w << 8) + v;
  66.                 r = LandScape[i].Vertex[p].vz;
  67.                 if(r <= 0.0)
  68.                     continue;
  69.                 d = (float)PERSPECTIVE / (float)r;
  70.                 LandScape[i].Vertex[p].sx = (long)(0.5 + LandScape[i].Vertex[p].vx * d + DD->HalfScreen.x);
  71.                 LandScape[i].Vertex[p].sy = (long)(0.5 + LandScape[i].Vertex[p].vy * d + DD->HalfScreen.y);
  72.             }
  73.         }
  74.     }
  75.  
  76.     return 0;
  77. };
  78.  
  79. int WORLD::BackfaceCull(POLYGON *p)
  80. {
  81.     float X1, Y1, Z1;
  82.     float X2, Y2, Z2;
  83.     float X3, Y3, Z3;
  84.     float D;
  85.  
  86.     X1 = p->Vertex[0].vx; Y1 = p->Vertex[0].vy; Z1 = p->Vertex[0].vz;
  87.     X2 = p->Vertex[1].vx; Y2 = p->Vertex[1].vy; Z2 = p->Vertex[1].vz;
  88.     X3 = p->Vertex[2].vx; Y3 = p->Vertex[2].vy; Z3 = p->Vertex[2].vz;
  89.  
  90.     D = (X3 * ((Z1 * Y2) - (Y1 * Z2)))
  91.       + (Y3 * ((X1 * Z2) - (Z1 * X2)))
  92.       + (Z3 * ((Y1 * X2) - (X1 * Y2)));
  93.  
  94.     return(D < 0);
  95. };
  96.  
  97. int WORLD::DrawWorld(void *Screen)
  98. {
  99.     int i;
  100.  
  101.     for(i = 0; i < Number_LandScapes; i++)
  102.         DrawLandScape(&LandScape[i], Screen);
  103.     return 0;
  104. };
  105.  
  106. int WORLD::DrawLandScape(LANDSCAPE *l, void *Screen)
  107. {
  108.     int i, j;
  109.     int r = 0;
  110.     int sort = 1;
  111.     int sx, ex;
  112.     int sz, ez;
  113.  
  114.     sx = Camera[Current_Camera].xTrans;
  115.     sz = Camera[Current_Camera].zTrans;
  116.     sx >>= 10;
  117.     sz >>= 10;
  118.     ex = ( ((sx + (X_DIR - 1)) < (l->Width - 1)) ? (sx + (X_DIR - 1)) : ((l->Width - 2)));
  119.     sx = ( ((sx - (X_DIR - 1)) < 0) ? (0) : (sx - (X_DIR - 1)));
  120.     ez = ( ((sz + (Z_DIR - 1)) < (l->Height - 1)) ? (sz + (Z_DIR - 1)) : ((l->Height - 2)));
  121.     sz = ( ((sz - (Z_DIR - 1)) < 0) ? (0) : (sz - (Z_DIR - 1)));
  122.  
  123.     Number_Polygons = 0;
  124.     for(i = sx; i < ex; i++)
  125.     for(int k = sz; k < ez; k++)
  126.     {
  127.         PolyList[Number_Polygons].Vertex[0] = l->Vertex[(k << 8) + i];
  128.         PolyList[Number_Polygons].Vertex[1] = l->Vertex[(k << 8) + i + 1];
  129.         PolyList[Number_Polygons].Vertex[2] = l->Vertex[((k + 1) << 8) + i];
  130.         PolyList[Number_Polygons].Number_Vertices = 3;
  131.         PolyList[Number_Polygons].TextPt[0].x = 0;
  132.         PolyList[Number_Polygons].TextPt[0].y = 0;
  133.         PolyList[Number_Polygons].TextPt[1].x = 31;
  134.         PolyList[Number_Polygons].TextPt[1].y = 0;
  135.         PolyList[Number_Polygons].TextPt[2].x = 0;
  136.         PolyList[Number_Polygons].TextPt[2].y = 31;
  137.         PolyList[Number_Polygons].Texture = *(ImageMap + (k << 8) + i);
  138.         if(!Reject(&PolyList[Number_Polygons]))
  139.         {
  140.             if(!BackfaceCull(&PolyList[Number_Polygons]))
  141.             {
  142.                 for(j = 0; j < PolyList[Number_Polygons].Number_Vertices; j++)
  143.                 {
  144.                     if(PolyList[Number_Polygons].Vertex[j].vz <= HITHER)
  145.                     {
  146.                         PolyList[Number_Polygons].Number_Vertices = 1;
  147. //                        zClipPolygon(&PolyList[Number_Polygons]);
  148.                         break;
  149.                     }
  150.                 }
  151.                 if(PolyList[Number_Polygons].Number_Vertices >= 3)
  152.                 {
  153.                     PolyList[Number_Polygons].Color = 0;
  154.                     Number_Polygons++;
  155.                 }
  156.             }
  157.         }
  158.  
  159.         PolyList[Number_Polygons].Vertex[0] = l->Vertex[(k << 8) + i + 1];
  160.         PolyList[Number_Polygons].Vertex[1] = l->Vertex[((k + 1) << 8) + i + 1];
  161.         PolyList[Number_Polygons].Vertex[2] = l->Vertex[((k + 1) << 8) + i];
  162.         PolyList[Number_Polygons].Number_Vertices = 3;
  163.         PolyList[Number_Polygons].TextPt[0].x = 31;
  164.         PolyList[Number_Polygons].TextPt[0].y = 0;
  165.         PolyList[Number_Polygons].TextPt[1].x = 31;
  166.         PolyList[Number_Polygons].TextPt[1].y = 31;
  167.         PolyList[Number_Polygons].TextPt[2].x = 0;
  168.         PolyList[Number_Polygons].TextPt[2].y = 31;
  169.         PolyList[Number_Polygons].Texture = *(ImageMap + (k << 8) + i);
  170.         if(!Reject(&PolyList[Number_Polygons]))
  171.         {
  172.             if(!BackfaceCull(&PolyList[Number_Polygons]))
  173.             {
  174.                 for(j = 0; j < PolyList[Number_Polygons].Number_Vertices; j++)
  175.                 {
  176.                     if(PolyList[Number_Polygons].Vertex[j].vz <= HITHER)
  177.                     {
  178.                         PolyList[Number_Polygons].Number_Vertices = 1;
  179. //                        zClipPolygon(&PolyList[Number_Polygons]);
  180.                         break;
  181.                     }
  182.                 }
  183.                 if(PolyList[Number_Polygons].Number_Vertices >= 3)
  184.                 {
  185.                     PolyList[Number_Polygons].Color = 0;
  186.                     Number_Polygons++;
  187.                 }
  188.             }
  189.         }
  190.     }
  191.  
  192.     if(Number_Polygons <= 1)
  193.         return(1);
  194.     SortPolyList(PolyList, 0, Number_Polygons - 1);
  195.  
  196.     for(i = 0; i < Number_Polygons; i++)
  197.     {
  198.         if(PolyList[i].Number_Vertices == 3)
  199.             DrawTriangle(&PolyList[i], Screen);
  200. //        else
  201. //            TexturedPolygon(&PolyList[i], Screen, FALSE);
  202.     }
  203.     return 0;
  204. };
  205.  
  206. int WORLD::SortPolyList(POLYGON *item, int left, int right)
  207. {
  208.     int i, j;
  209.     float x;
  210.     POLYGON y;
  211.  
  212.     i = left; j = right;
  213.     x = item[(left + right) >> 1].Vertex[0].vz;
  214.  
  215.     do
  216.     {
  217.         while(item[i].Vertex[0].vz > x && i < right) i++;
  218.         while(x > item[j].Vertex[0].vz && j > left) j--;
  219.  
  220.         if(i <= j)
  221.         {
  222.             y = item[i];
  223.             item[i] = item[j];
  224.             item[j] = y;
  225.             i++; j--;
  226.         }
  227.  
  228.     } while(i <= j);
  229.  
  230.     if(left < j)
  231.         SortPolyList(item, left, j);
  232.     if(i < right)
  233.         SortPolyList(item, i, right);
  234.     return 0;
  235. };
  236.  
  237. int WORLD::UpdateWorld()
  238. {
  239.     int i;
  240.     int sx, ex;
  241.     int sz, ez;
  242.     int dx, dz;
  243.     int p1, p2, p3, p4;
  244.     int xrem, zrem;
  245.     VECTOR a, b, c;
  246.     float cosa, sina;
  247.                 
  248.     sx = Camera[Current_Camera].xTrans;
  249.     sz = Camera[Current_Camera].zTrans;
  250.  
  251.     xrem = sx & 1023;
  252.     zrem = sz & 1023;
  253.     
  254.     sx >>= 10;
  255.     sz >>= 10;
  256.  
  257.     if(    Camera[Current_Camera].yAngle >= -1.57 &&
  258.         Camera[Current_Camera].yAngle <= 1.57 )
  259.         ex = sx + 1;
  260.     else
  261.         ex = sx - 1;
  262.     if(    Camera[Current_Camera].yAngle >= 0 &&
  263.         Camera[Current_Camera].yAngle <= 3.16 )
  264.         ez = sz + 1;
  265.     else
  266.         ez = sz - 1;
  267.  
  268.     sx = (sx < 0) ? 0 : (sx > 255) ? 255 : sx;
  269.     ex = (ex < 0) ? 0 : (ex > 255) ? 255 : ex;
  270.     sz = (sz < 0) ? 0 : (sz > 255) ? 255 : sz;
  271.     ez = (ez < 0) ? 0 : (ez > 255) ? 255 : ez;
  272.  
  273.     p1 = (sz << 8) + sx;
  274.     p2 = (sz << 8) + ex;
  275.     p3 = (ez << 8) + sx;
  276.     p4 = (ez << 8) + ex;
  277.         
  278.     p1 = LandScape[0].Vertex[p1].oy;
  279.     p2 = LandScape[0].Vertex[p2].oy;
  280.     p3 = LandScape[0].Vertex[p3].oy;
  281.     p4 = LandScape[0].Vertex[p4].oy;
  282.  
  283.     if(p1 < p2)
  284.         dz = p1;
  285.     else
  286.         dz = p2;
  287.     if(p3 < p4)
  288.         dx = p3;
  289.     else
  290.         dx = p4;
  291.     if(dz > dx)
  292.         dz = dx;
  293.  
  294.     Camera[Current_Camera].yTrans = dz - 640;
  295.     
  296.     UpdateMatrix->Identity();
  297.     UpdateMatrix->Scale(1, 1, 1);
  298.     UpdateMatrix->Translate(-Camera[Current_Camera].xTrans,
  299.                             -Camera[Current_Camera].yTrans,
  300.                             -Camera[Current_Camera].zTrans);
  301.     UpdateMatrix->Rotate(-Camera[Current_Camera].xAngle,
  302.                          -Camera[Current_Camera].yAngle,
  303.                          -Camera[Current_Camera].zAngle);
  304.  
  305.     for(i = 0; i < Number_LandScapes; i++)
  306.         UpdateMatrix->TransformWithCamera(&LandScape[i]);
  307.     return 0;
  308. };
  309.  
  310. int Reject(POLYGON *p)
  311. {
  312.     int l, r, u, d, z;
  313.     int n;
  314.  
  315.     l = r = u = d = 0;
  316.     n = p->Number_Vertices;
  317.     for(int i = 0; i < n; i++)
  318.     {
  319.         if(p->Vertex[i].sx < 0)
  320.             l++;
  321.         if(p->Vertex[i].sx >= DD->Screen.x)
  322.             r++;
  323.         if(p->Vertex[i].sy < 0)
  324.             u++;
  325.         if(p->Vertex[i].sy >= DD->Screen.y)
  326.             d++;
  327.         if(p->Vertex[i].vz < HITHER)
  328.             z++;
  329.     }
  330.  
  331.     if(l == n || r == n || u == n || d == n || z == n)
  332.         return 1;
  333.     else
  334.         return 0;
  335. };
  336.